﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
using System.Windows;

namespace Freshome.m.mds
{
    public abstract class Modeland : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            this.VerifyPropertyName(propertyName);
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        [Conditional("DEBUG")]
        [DebuggerStepThrough]
        private void VerifyPropertyName(string propertyName)
        {
            // Verify that the property name matches a real, public, instance property on this object.
            if (TypeDescriptor.GetProperties(this)[propertyName] == null)
            {
                string msg = "Invalid property name: " + propertyName;
                Debug.Fail(msg);
            }
        }
        private bool _updated = false;
        public bool Updated
        {
            get { return this._updated; }
            set
            {
                if (this._updated != value)
                {
                    this._updated = value;
                    OnPropertyChanged("Updated");
                }
            }
        }
        public abstract bool Created { get; }
    }
    public static class Bitmaport
    {
        public static BitmapImage Imaget(byte[] imageData)
        {
            if (imageData != null)
            {
                MemoryStream ms = new MemoryStream(imageData);
                ms.Seek(0, SeekOrigin.Begin);
                BitmapImage image = new BitmapImage();
                image.BeginInit();
                image.StreamSource = ms;
                image.EndInit();
                return image;
            }
            else return null;
        }
        public static byte[] InitialImaget(out string imageType)
        {
            StreamResourceInfo sri = Application.GetResourceStream(new Uri("/a.jpg", UriKind.Relative));
            byte[] buf = new byte[sri.Stream.Length];
            sri.Stream.Read(buf, 0, (int)sri.Stream.Length);
            imageType = "jpg";
            return buf;
        }
    }
    internal static class Extensions
    {
        public static bool Match(this string tx, string fm)
        {
            Regex ex = new Regex(fm);
            return ex.Match(tx).Success;
        }
    }
    public abstract class Shortype<T> : Modeland where T : Rmt.Shortype
    {
        protected T _pm;
        public Shortype(T pm)
        {
            this._pm = pm;
        }
        public override bool Created
        {
            get { return _pm.Mid <= default(short); }
        }
        public short Mid
        {
            get { return this._pm.Mid; }
            set
            {
                if (this._pm.Mid != value)
                {
                    this._pm.Mid = value;
                    OnPropertyChanged("Created");
                }
            }
        }
        public string Stamp
        {
            get { return this._pm.Stamp; }
            set
            {
                value = value.Trim();
                if (value.Match(@"^[\S\s]{0,50}$") && this._pm.Stamp != value)
                {
                    this._pm.Stamp = value;
                }
                OnPropertyChanged("Stamp");
            }
        }
        public string Notes
        {
            get { return this._pm.Notes; }
            set
            {
                value = value.Trim();
                if (value.Match(@"^[\S\s]{0,50}$") && this._pm.Notes != value)
                {
                    this._pm.Notes = value;
                    this.Updated = !Created;
                }
                OnPropertyChanged("Notes");
            }
        }
        public T Raw
        {
            get { return this._pm; }
        }
        public abstract void Refresh(T pm);
    }
}
